import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../common/const/app_color_const.dart';
import '../common/util/common_util.dart';

class RefreshListIndicator extends StatefulWidget {
  final Future<void> Function()? onRefresh;
  final Future<void> Function()? onLoadMore;
  final bool isLastPage;
  final Widget child;
  final String? noMoreText;
  final TextStyle? noMoreTextStyle;
  final int scrollOffset;

  const RefreshListIndicator({
    Key? key,
    required this.child,
    required this.isLastPage,
    this.noMoreText,
    this.noMoreTextStyle,
    this.scrollOffset = 10,
    this.onRefresh,
    this.onLoadMore,
  }) : super(key: key);

  @override
  RefreshListIndicatorState createState() => RefreshListIndicatorState();
}

class RefreshListIndicatorState extends State<RefreshListIndicator> {
  final GlobalKey<RefreshIndicatorState> refreshIndicatorKey =
      GlobalKey<RefreshIndicatorState>();

  ScrollController? scrollController;
  bool isLoading = false;

  @override
  void initState() {
    super.initState();
    scrollController = ScrollController();
    scrollController!.addListener(() async {
      if (scrollController!.offset <= widget.scrollOffset) {
        return;
      }

      CommonUtil.debounce(() async {
        if (scrollController!.position.pixels >=
            scrollController!.position.maxScrollExtent) {
          if (isLoading) {
            return;
          }
          if (mounted) {
            setState(() {
              isLoading = true;
            });
          }
          if (!widget.isLastPage && widget.onLoadMore != null) {
            await widget.onLoadMore!();
          }

          if (mounted) {
            setState(() {
              isLoading = false;
            });
          }
        }
      }, null)();
    });
  }

  @override
  void dispose() {
    scrollController!.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Widget mainWidget = ListView(
      physics:
          const BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
      controller: scrollController,
      children: <Widget>[
        widget.child,
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(16),
              child: isLoading
                  ? const CupertinoActivityIndicator()
                  : Text(
                      widget.isLastPage
                          ? widget.noMoreText ?? '---这已经是我的底线了---'
                          : '',
                      style: widget.noMoreTextStyle ??
                          const TextStyle(
                            fontSize: 13,
                            color: grayColor,
                          ),
                    ),
            )
          ],
        )
      ],
    );

    if (widget.onRefresh == null) {
      return Scrollbar(child: mainWidget);
    }

    return RefreshIndicator(
      key: refreshIndicatorKey,
      onRefresh: () async {
        if (isLoading) {
          return;
        }

        if (widget.onRefresh != null) {
          await widget.onRefresh!();
        }
      },
      child: mainWidget,
    );
  }
}
